package com.atfusu.controller;

import com.atfusu.base.BaseController;
import com.atfusu.bean.entity.*;
import com.atfusu.service.*;
import com.github.pagehelper.PageInfo;
import jline.WindowsTerminal;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Title:
 * Author: fusu
 * DateTime:  2022/11/29
 * Description:
 **/
@Controller
@RequestMapping("/house")
public class HouseController extends BaseController {

    private static final String PAGE_INDEX = "house/index";
    private static final String PAGE_CREATE = "house/create";
    private static final String PAGE_SUCCESS = "common/success";
    private static final String PAGE_EDIT = "house/edit";
    private static final String PAGE_SHOW = "house/show";

    @DubboReference
    private HouseService houseService;

    @DubboReference
    private CommunityService communityService;

    @DubboReference
    private DictService dictService;

    @DubboReference
    private HouseImageService houseImageService;

    @DubboReference
    private HouseBrokerService houseBrokerService;

    @DubboReference
    private HouseUserService houseUserService;

    @RequestMapping
    public String findPage(HttpServletRequest request, Map map) {
        Map<String, Object> filters = getFilters(request);
        PageInfo<House> page = houseService.findPage(filters);
        map.put("page", page);
        map.put("filters", filters);
        //添加查询条件的集合
        getSources(map);

        return PAGE_INDEX;
    }

    @RequestMapping("/create")
    public String create(Map map) {
        //添加查询条件的map集合
        getSources(map);

        return PAGE_CREATE;
    }

    @RequestMapping("/save")
    public String save(House house) {
        houseService.insert(house);

        return PAGE_SUCCESS;
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Map map) {
        getSources(map);
        House house = houseService.getById(id);
        map.put("house", house);
        return PAGE_EDIT;
    }

    @RequestMapping("/update")
    public String update(House house) {
        houseService.update(house);
        return PAGE_SUCCESS;
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        houseService.delete(id);
        return "redirect:/house";
    }

    @RequestMapping("/publish/{id}/{status}")
    public String publish(@PathVariable Long id, @PathVariable Integer status) {
        houseService.publish(id, status);
        return "redirect:/house";
    }

    /**
     * 详情页面
     */
    @RequestMapping("/show/{id}")
    public String show(@PathVariable Long id, Map map) {
        //house对象
        House house = houseService.showById(id);
        //community对象
        Community community = communityService.showById(house.getCommunityId());
        //houseImage1List集合（type=1的houseImage集合）
        List<HouseImage> houseImage1List = houseImageService.findListByHouseIdAndType(id, 1);
        //houseImage2List集合（type=2的houseImage集合）
        List<HouseImage> houseImage2List = houseImageService.findListByHouseIdAndType(id, 2);
        //houseBrokerList集合（经纪人信息）
        List<HouseBroker> houseBrokerList = houseBrokerService.findListByHouseId(id);
        //houseUserList集合（房东信息）
        List<HouseUser> houseUserList = houseUserService.findListByHouseId(id);

        //将结果放入请求域
        map.put("house",house);
        map.put("community",community);
        map.put("houseImage1List",houseImage1List);
        map.put("houseImage2List",houseImage2List);
        map.put("houseBrokerList",houseBrokerList);
        map.put("houseUserList",houseUserList);

        return PAGE_SHOW;
    }


    /**
     * 用于获取相关查询信息并添加到map集合
     */
    protected void getSources(Map map) {
        //小区
        List<Community> communityList = communityService.findAll();
        //户型
        List<Dict> houseTypeList = dictService.findListByDictCode("houseType");
        //楼层
        List<Dict> floorList = dictService.findListByDictCode("floor");
        //建筑结构
        List<Dict> buildStructureList = dictService.findListByDictCode("buildStructure");
        //朝向
        List<Dict> directionList = dictService.findListByDictCode("direction");
        //装修情况
        List<Dict> decorationList = dictService.findListByDictCode("decoration");
        //房屋用途
        List<Dict> houseUseList = dictService.findListByDictCode("houseUse");
        //放入map集合
        map.put("communityList", communityList);
        map.put("houseTypeList", houseTypeList);
        map.put("floorList", floorList);
        map.put("buildStructureList", buildStructureList);
        map.put("directionList", directionList);
        map.put("decorationList", decorationList);
        map.put("houseUseList", houseUseList);
    }
}
